package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.SchoolLinkEntity;
import cn.eclass.utils.Helper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class SchoolLinkDao extends CommonDao {

    private static final String TABLE_NAME = "schoollink";

    public SchoolLinkDao() {
        super(SchoolLinkDao.class.getName());
    }

    public List<SchoolLinkEntity> getSchoolLinkByCat(int catId, int offSet, int limit) {
        String sql = "select * from " + TABLE_NAME + " where category_id = " + catId + " limit " + offSet + "," + limit;
        List list = super.select(sql);
        return list;
    }

    public SchoolLinkEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<SchoolLinkEntity> entitys = (List<SchoolLinkEntity>) super.select(sql);
        return entitys.get(0);
    }

    public int add(SchoolLinkEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + "(`link`,`link_picture`,`link_description`,`link_name`,`category_id`,`school_id`, `order_amount`) "
                + "values ("
                + "'" + entity.getLink() + "',"
                + "'" + entity.getLinkPicture() + "',"
                + "'" + entity.getLinkDescription() + "',"
                + "'" + entity.getLinkName() + "',"
                + entity.getCategoryId() + ","
                + entity.getSchoolId() + ","
                + entity.getOrderAmount()
                + ")";
        return super.insert(sql);
    }

    public List<SchoolLinkEntity> getSchoolLinkByCat(List<Integer> catIdList, int offSet, int limit) {
        String catList = "";
        String catIdListStr = Helper.intList2Str(catIdList);
        String sql = "select * from " + TABLE_NAME + " where category_id in (" + catIdListStr + ") limit " + offSet + "," + limit;
        List list = super.select(sql);
        return list;
    }

    public int getItemCountByCat(int catId) {
        String sql = "select count(*) from " + TABLE_NAME + " where category_id = " + catId;
        return super.getItemCount(sql);
    }

    public int getItemCountByCat(List<Integer> catIdList) {
        String catList = "";
        String catIdListStr = Helper.intList2Str(catIdList);
        String sql = "select count(*) from " + TABLE_NAME + " where category_id in (" + catIdListStr + ")";
        return super.getItemCount(sql);
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        SchoolLinkEntity sle = new SchoolLinkEntity();
        try {
            sle.setId(resultSet.getInt("ID"));
            sle.setCategoryId(resultSet.getInt("category_id"));
            sle.setLink(resultSet.getString("link"));
            sle.setLinkDescription(resultSet.getString("link_description"));
            sle.setLinkName(resultSet.getString("link_name"));
            sle.setLinkPicture(resultSet.getString("link_picture"));
            sle.setSchoolId(resultSet.getInt("school_id"));
            sle.setOrderAmount(resultSet.getInt("order_amount"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return sle;
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        super.delete(sql);
        return true;
    }

    public boolean update(SchoolLinkEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`link` = '" + entity.getLink() + "',"
                + "`link_picture` = '" + entity.getLinkPicture() + "',"
                + "`link_description` = '" + entity.getLinkDescription() + "',"
                + "`link_name` = " + entity.getLinkName() + ","
                + "`category_id` = " + entity.getCategoryId() + ","
                + "`school_id` = " + entity.getSchoolId() + ","
                + "`order_amount` = " + entity.getOrderAmount()
                + " where id = " + entity.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public List<SchoolLinkEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<SchoolLinkEntity>) super.select(sql);
    }

    public int countSearch(String search) {
        String sql = "select count(*) as count " + getSearchFrom(search);
        int count = Integer.valueOf(super.select2Map(sql).get(0).get("count"));
        return count;
    }

    public List<SchoolLinkEntity> search(String search, int offSet, int itemPerPage) {

        String sql = "select * " + getSearchFrom(search) + " limit " + offSet + ",  " + itemPerPage;
        return (List<SchoolLinkEntity>) super.select(sql);
    }

    private String getSearchFrom(String search) {
        String sql = " from " + TABLE_NAME + " where  link_name like '%" + search + "%' or link_description like '%" + search + "%'";
        return sql;
    }
}
